<template>
  <a-modal
    :title="title"
    width="70%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @cancel="()=>{this.visible=false}"
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="客户" prop="MID">
          <a-select
                allowClear
                show-search
                v-model="entity.MID"
                placeholder="请选择客户"
              >
                <a-select-option v-for="(item, index) in MerchantList" :key="index" :value="item.value">
                  {{ item.text }}
                </a-select-option>
              </a-select>
        </a-form-model-item>
        <a-form-model-item label="收件人姓名" prop="RecipientName">
          <a-input v-model="entity.RecipientName" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="电话" prop="Phone">
          <a-input v-model="entity.Phone" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="收件地址" prop="RecipientAddr">
          <a-input v-model="entity.RecipientAddr" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="是否默认" prop="IsDefault">
        <a-radio-group default-value="1" button-style="solid" v-model="entity.IsDefault">
        <a-radio-button :value="1">
          是
        </a-radio-button>
        <a-radio-button :value="0">
          否
        </a-radio-button>
      </a-radio-group>
        </a-form-model-item>
        <!-- <a-form-model-item label="是否默认" prop="IsDefault">
          <a-input v-model="entity.IsDefault" autocomplete="off" />
        </a-form-model-item> -->
        <!-- <a-form-model-item label="数据权限" prop="DataAreaId">
          <a-input v-model="entity.DataAreaId" autocomplete="off" />
        </a-form-model-item> -->
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
export default {
  props: {
    parentObj: Object
  },
  data() {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {
        MID:[{ required: true, message: '请选择客户' }],
        RecipientName:[{ required: true, message: '请输入收件人' }],
        Phone:[{ required: true, message: '请输入联系方式' }],
        RecipientAddr:[{ required: true, message: '请输入收获地址' }],
      },
      MerchantList:[],
      title: '新增'
    }
  },
  methods: {
    init() {
      this.GetMerchant()
      this.title = "新增"
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    async GetMerchant() {
      await this.$http.post('/Base_Manage/Home/GetMerchant_Select').then((res) => {
        this.MerchantList = res.Data
      })
    },
    openForm(id, title) {
      this.init()

      if (id) {
        this.title = "修改"
        this.loading = true
        this.$http.post('/Base_leasesystem_billingaddress/leasesystem_billingaddress/GetTheData', { id: id }).then(resJson => {
          this.loading = false

          this.entity = resJson.Data
        })
      }
    },
    handleSubmit() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        this.$http.post('/Base_leasesystem_billingaddress/leasesystem_billingaddress/SaveData', this.entity).then(resJson => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false

            this.parentObj.GetAddress(this.entity)
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    }
  }
}
</script>
